$(document).ready(function (){
	StatisticalInformation();//获取页面中带有统计信息的字段 ，class=localCache
	StatisticsRCCount();//统计入场信息
	getIntoRoomUrl();//进入房间的URL
	WordWrap("lenCon",41);
});
//JS容错代码，不提示错误。
window.onerror=function (){return true;}
//动态改变房间进入房间的URL
function getIntoRoomUrl(){
	
	$(".intoroom").each(function (i,n){
		$(n).unbind("click");
		$(n).click(function (){
			var id = $("#room_id").val();
			var title = $(this).attr("title");
			if(title!=undefined){
				id = title;
			}
			var oemid = $("#oemid").val();
			$("<iframe src=\"/room/intoroom.jsp?oemid="+oemid+"&id="+id+"\"  width=\"0\" height=\"0\" style=\"display:none;\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\"/>").prependTo('body');
			return false;  
		});
	});
}



	

//查询统计信息
function StatisticalInformation(){
	//i 顺序, n 对象
	$(".localCache").each(function (i,n){
		//alert(n);
		var controlId = $(n).attr("id");
		var roomid = controlId.substring(controlId.indexOf("_")+1);
		$.ajax({
			type: "POST",		
			url: "/room!getLocalCache.jspa?roomid="+roomid, 
			dataType : "json",
       		complete : function (data, textStatus){
       			if(eval("(" + data.responseText + ")").result==0){
       				$(n).html(eval("(" + data.responseText + ")").content);
       			}
			}
		});
	});
}
/*跳转到房间信息编辑页面*/
function toroomedit(roomid){
	window.location.href="/room!toRoomEdit.jspa?roomid="+roomid;
}

function AddFavorite(sURL, sTitle) {   
    try {   
        window.external.addFavorite(sURL, sTitle);   
    } catch (e) {   
        try {   
            window.sidebar.addPanel(sTitle, sURL, "");   
        } catch (e) {   
            alert("Join collection failure, please use the Ctrl + D to add");   
        }   
    }
} 

//添加房间收藏
function favroom(roomid,isLocalFavorite){
//		var tid = $(obj).attr("id");
//		var roomid = tid.substring(tid.indexOf("_")+1);
		if(isLocalFavorite){
			AddFavorite(window.location.href,"\u5471\u5471\u89c6\u9891");
			$("#favroom_"+roomid).html("<i></i><span>\u5df2\u6536\u85cf</span>");
			$("#favroom_"+roomid).css("text-decoration","none");
			$("#favroom_"+roomid).attr("cursor","default");
			$("#favroom_"+roomid).removeClass("but1");
			$("#favroom_"+roomid).addClass("but1On");
			$("#favroom_"+roomid).attr("href","#");
			return ;
		}
		$.ajax({
			type: "POST",		
			url: "/room!addfavroom.jspa?roomid="+roomid, 
			dataType : "json",
       		complete : function (data, textStatus){
       			if(eval("(" + data.responseText + ")").result==0){
       				$("#favroom_"+roomid).html("<i></i><span>\u5df2\u6536\u85cf</span>");
       				$("#favroom_"+roomid).css("text-decoration","none");
       				$("#favroom_"+roomid).css("cursor","default");
       				$("#favroom_"+roomid).removeClass("but1");
       				$("#favroom_"+roomid).addClass("but1On");
       				$("#favroom_"+roomid).attr("href","#");
       				var i = parseInt($("#localRoomCollect_"+roomid).html())+1;
       				$("#localRoomCollect_"+roomid).html(i);
       			}else if(eval("(" + data.responseText + ")").result==4){
       				$("#favroom_"+roomid).html("<i></i><span>\u5df2\u6536\u85cf</span>");
       				$("#favroom_"+roomid).css("text-decoration","none");
       				$("#favroom_"+roomid).css("cursor","default");
       				$("#favroom_"+roomid).removeClass("but1");
       				$("#favroom_"+roomid).addClass("but1On");
       				$("#favroom_"+roomid).attr("href","#");
       			}
			}
		});
}
/*报名*/
function subJoiner(activeID,roomid)
{
   var oemid = $('#oemid').val();
   var request = $.ajax(
    {
        url : "/room!addJoiner.jspa", type : "post", data : {
            "active.id" : activeID, "active.room_id" :roomid ,"active.start_time":$("#starTimeLong_"+activeID).val()
        },
        dataType : "json",
        complete : function (data, textStatus)
        {
        	var result = eval("(" + data.responseText + ")").result;
            if (result == 0)
            {
                $("#joinAmount" + activeID).html(Number($("#joinAmount" + activeID).html()) + 1);
                //\u4FEE\u6539\u56FE\u7247\u663E\u793A
                //$("#joinerStat" + activeID).attr("class", "activeCont0002");
                $("#preactiveA" + activeID).attr("href", "javascript:void(0);");
                $("#preactiveA" + activeID).html("\u5df2\u62a5\u540d");
                $("#preactiveA" + activeID).removeClass("but1");
                $("#preactiveA" + activeID).addClass("but2");
                
                //\u5220\u9664cookie
                delCookie("joinerStat_" + activeID + "_" + roomid + "_" + $('#userid').val());
                //动态无刷加入报名图标
				//$("#bmImg_"+activeID).replaceWith("<div class=\"ltImg-txt3\" id=\"p_img1_"+activeID+"\"></div>");                
            }//10分钟内入场
            else if (result ==11) {
            	var href= "http://chat.17guagua.net/enterroom.jsp?id="+roomid+"&oemid="+oemid;
                window.open(href);
            }//结束
            else if (result==12) {
                alert("\u6d3b\u52a8\u5df2\u7ecf\u7ed3\u675f\u3002");
            }
            else if (result==- 1) {
                alert("\u7528\u6237\u672A\u767B\u5F55");
            }
            else if (result==- 2) {
                alert("\u62a5\u540d\u5931\u8d25");
            }
            else {
                //alert("\u7CFB\u7EDF\u9519\u8BEF");
                alert(result);
            }
        }
    });
}
function checkAll(obj){
	if($(obj).attr("checked")){
		$("input[name='des_room_id']").each(function (i,obj){
			if($(obj).attr("disabled")!="disabled"){
				$(obj).attr("checked","checked");
			}
		});
	}else{
		$("input[name='des_room_id']").each(function (i,obj){
			if($(obj).attr("disabled")!="disabled"){
				$(obj).removeAttr("checked");
			}
		});		
	}
}

var zbObjs = new Array();
function initZB(){
		$.ajax({
			type: "POST",		
			url: "/room!getZBInfo.jspa", 
			dataType :"application/json;charset=utf-8",	
			async: false,
	      	complete : function (data){
	      		var roomVoList = eval("(" + data.responseText + ")").roomVoList;
	      		var len = eval("(" + data.responseText + ")").roomVoListSize;
		      	if (len != 0) {
		      		var str = "";
					for (var i = 0; i < len; i++) {
						if(roomVoList[i]["roomId"]!=$("#room_id").val()){
							str+="<li><label><input type=\"checkbox\" name = \"des_room_id\" value=\""+roomVoList[i]["roomId"]+"\"/>"+roomVoList[i]["roomId"]+" "+roomVoList[i]["roomName"]+"</label></li>";
						}
					}
					$("#zbUl").html(str);
					$("#roomVoListSize").val(len);
				}	
			}
		});
}
/*转播*/
function replyActive(activeID,isMaster,roomid,userid,hisRoomid)
{
	//只有室主才能转播,游客不能转播
	if(userid<=0){alert("\u53ea\u6709\u5ba4\u4e3b\u624d\u53ef\u4ee5\u8f6c\u64ad\u3002");return;}
	//本房间室主不可以转播
	//if(isMaster==1){return;}
	initZB();	
	//只有有房间才会出现转播的东西
	if($("#roomVoListSize").val()==0){alert("\u53ea\u6709\u5ba4\u4e3b\u624d\u53ef\u4ee5\u8f6c\u64ad\u3002");return;}
	var des_room_id = "";
	$.ajax({
			type: "POST",		
			url: "/room!getBroadcastByUid.jspa", 
			dataType :"application/json;charset=utf-8",	
			data:{"roomid":roomid,"activeid":activeID},
	      	complete : function (data, textStatus){
	      		var ids = eval("(" + data.responseText + ")").result;
		      	$("input[name='des_room_id']").each(function (i,obj){
		      		if(ids.indexOf($(obj).val())>=0){
	      				$(obj).attr("disabled","disabled");
	      				$(obj).attr("checked","checked");
	      			}else{
	      				$(obj).removeAttr("disabled");
	      				$(obj).removeAttr("checked");
	      			}
				});
			}
		});
	//显示DIV，
	showPopWin(this,'roomVoPop',function(){},'center');
	$("#roomVoSubmit").unbind("click");
	$("#roomVoSubmit").click(function (){
		var des_room_id = "";
		var objArray = new Array();
		var index = 0;
		var isCheck = false;
		$("input[name='des_room_id']").each(function (i,obj){
			if($(obj).attr("checked")){
				isCheck=true;
			}
			if($(obj).attr("checked")&&$(obj).attr("disabled")!="disabled"){
				des_room_id=des_room_id+"&des_room_id="+$(obj).val();
				objArray[index] = obj;
				index = index +  1;
			}
		});
		if(des_room_id==""){
			if(!isCheck){
				hidePopWin('roomVoPop');
				//alert("\u8bf7\u9009\u62e9\u8981\u8f6c\u64ad\u5230\u54ea\u4e2a\u623f\u95f4");//请选择房间
				return;
			}else{
				alert("\u5168\u90e8\u623f\u95f4\u5df2\u7ecf\u8f6c\u64ad\u8fc7\u4e86\u3002");//全部房间已经转播过了。
				hidePopWin('roomVoPop');
				return;
			}
		}
		$.ajax({
			type: "POST",		
			url: "/room!saveBroadcast.jspa?roomid="+roomid+des_room_id+"&activeid="+activeID, 
			dataType :"application/json;charset=utf-8",	
			//data:{"des_room_id":+des_room_id,"roomid":roomid,"activeid":activeID},
	      	complete : function (data, textStatus){
	      		for(var i =0 ;i <objArray.length;i++){
	      			$(objArray[i]).attr("disabled","disabled");
	      		}
				hidePopWin('roomVoPop');
			}
		});
   		return false;
	});
}
/*获取Cookie*/
function getCookie(name)//\u53D6cookies\u51FD\u6570        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) {
        return unescape(arr[2]);
    }
    return null;
}
/*删除Cookie,似乎没什么用。*/
function delCookie(name)//\u5220\u9664cookie

{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}

//del
function subdelActive(activeId,photoid)
{
/*
$(".imgTD").each(function (i,n){
						var tdid = $(n).attr("id");
						if("actice" + activeId == tdid){
							$("#idNum").find("li").eq(i).remove();
							len = $(".lunbo .ltImg").size();
							st = new SlideTrans("idContainer2", "idSlider2", len, { Vertical: false });
							//st.Stop(); $("#idNum").find("li").eq(len-2).addClass("on"); st.Auto = false; st.Run(len-2);
						}
					});
					$("#actice" + activeId).remove();
	return;
*/
    var bln = window.confirm("\u786E\u5B9A\u8981\u5220\u9664\u6D3B\u52A8\u4FE1\u606F\u5417?"); 
	if(bln){
		var request = $.ajax(
		{
			url : "/room!delActive.jspa", type : "post", data : 
			{
				"active.id" : activeId,"active.room_id" : $('#room_id').val(),
				"photo.id" : photoid
			},
			dataType : "json",
			complete : function (data, textStatus)
			{
				var result = eval("(" + data.responseText + ")").result ;
				if (result== 0)
				{   
				    if($("#preactiveAvatar" + activeId).attr("src")!=null&&$("#preactiveAvatar" + activeId).attr("src")!=""){
					   delImg($("#preactiveAvatar" + activeId).attr("src"));
					}
					
					$(".imgTD").each(function (i,n){
						var tdid = $(n).attr("id");
						if("#actice" + activeId == tdid){
							$("#idNum li").eq(i+1).remove();
						}
					});
					window.location.reload();
					//$("#actice" + activeId).remove();
				}else if(result==-1){
					alert("\u7528\u6237\u672a\u767b\u5f55");
				}else if(result==-2){
					alert("\u7528\u6237\u4e0d\u662f\u5ba4\u4e3b");
				}
				else {
					//alert(result);
					alert("\u7CFB\u7EDF\u9519\u8BEF\uFF0C\u5220\u9664\u6D3B\u52A8\u5931\u8D25\uFF01");
				}
			}
		});
	}
}

//zbDel
function zbDelActive(activeId,srcRoomid,desRoomid)
{
    var bln = window.confirm("\u786e\u5b9a\u53d6\u6d88\u8f6c\u64ad?"); 
	if(bln){
		var request = $.ajax(
		{
			url : "/room!zbDelActive.jspa", type : "post", data : 
			{
				"activeid" : activeId,"srcRoomid" : srcRoomid,
				"desRoomid" : desRoomid
			},
			dataType : "json",
			complete : function (data, textStatus)
			{
				if (eval("(" + data.responseText + ")").result == 0)
				{   
				    if($("#preactiveAvatar" + activeId).attr("src")!=null&&$("#preactiveAvatar" + activeId).attr("src")!=""){
					   delImg($("#preactiveAvatar" + activeId).attr("src"));
					}
					
					window.location.reload();
					//$("#actice" + activeId).remove();
					
				}
				else {
					alert("\u7CFB\u7EDF\u9519\u8BEF\uFF0C\u5220\u9664\u6D3B\u52A8\u5931\u8D25\uFF01");
				}
			}
		});
	}
}
function delImg(srcImg)
{
    var index = srcImg.indexOf("?");
    if (index > 0) {
        srcImg = srcImg.substring(0, index);
    }
    var request = $.ajax(
    {
        url : "/room!delImg.jspa", type : "post", data : {
            imgsrc : srcImg
        },
        dataType : "json", complete : function (data, textStatus) { }
    });
}
//edit

function editActive(activeId,room_id){
	window.location.href="/room!toActiveEdit.jspa?activeid="+activeId+"&roomid="+room_id;
}
function gotoActive(roomid,oemid){
	window.open("http://chat.17guagua.net/enterroom.jsp?id="+roomid+"&oemid="+oemid);
}
function gotoActivePage(roomid,oemid,activeId){
	window.location.href = "/room!activelist.jspa?roomid="+roomid+"&oemid="+oemid+"&activeid="+activeId;
}
//系统加载实现入场信息
function StatisticsRCCount(){
	/*
	var activeIds = "";
	var activeIdString = "";
	//i 顺序, n 对象
	$(".RCFlag").each(function (i,n){
		var controlId = $(n).attr("id");
		var activeId = controlId.replace("preactiveA","");
		var countControlId = "joinAmount"+activeId;
		activeIds+=","+activeId;
		activeIdString += "&activeIds="+activeIds;
	});
	if(activeIds.length>0){activeIds = activeIds.substring(1);}
	if(activeIdString.length>0){activeIdString = activeIdString.substring(1);}
	$.ajax({
			type: "POST",		
			url: "/room!getRCCount.jspa?"+activeIdString, 
			dataType : "json",
       		complete : function (data, textStatus){
       			if(eval("(" + data.responseText + ")").result==0){
       				$(n).html(eval("(" + data.responseText + ")").content);
       			}
			}
		});
	*/
	if($(".RCFlag").size()<=0){return ;}
	$.ajax({
		type: "POST",		
		url: "/room!getRCCount.jspa?roomid="+$("#room_id").val(), 
		dataType : "json",
   		complete : function (data, textStatus){
   			if(data.responseText==""){return;}
   			if(eval("(" + data.responseText + ")").result==0){   				
   				var count = eval("(" + data.responseText + ")").content;
   				if(count==""){count=0}
   				$(".RCFlag").each(function (i,n){
					var controlId = $(n).attr("id");
					var activeId = controlId.replace("preactiveA","");
					var countControlId = "joinAmount"+activeId;
					$("#"+countControlId).html(count);
				});
   			}
		}
	});
	
}
function WordWrap(className,textlength){
	if(window.XMLHttpRequest){//判断浏览器是否属于Mozilla,Sofari
		$("."+className).each(function (){
			var strText=$(this).html();
			var tem="";
			while(strText.length>textlength){
			tem+=strText.substr(0,textlength)+"<br/>";
			strText=strText.substr(textlength,strText.length);
			}
			tem+= strText;
			$(this).html(tem);
		});
	}
}
String.prototype.replaceAll = function(s1,s2) { 
//    return this.replace(new RegExp(s1,"gm"),s2); 
	var content = this.replace("","");
	while(content.indexOf(s1)!=-1){
		content = content.replace(s1, s2);
	}
	return content;
}
function forceWriteHTML(content){
	content = content.replaceAll("<img src=\"/images/emoticons/","[IMGFACE]");
	content = content.replaceAll("</img>","[/IMGFACE]");
	content = content.replaceAll("<","&lt;");
	content =  content.replaceAll(">","&gt;");
	content = content.replaceAll("[IMGFACE]","<img src=\"/images/emoticons/");
	content = content.replaceAll("&gt;[/IMGFACE]","></img>");
	return content;

}


